<template>
  <div class="portBox">
      <!-- 修改头像页面   -->
    <div class="portimgBox">
        <div class="portimg">
            <img :src="imgUrl" alt="">
        </div>
    </div>
    <p class="errRes">{{errInfo}}</p>
    <div class="portFun">
        <input type="file" class="portinp" @change="uploadImg">
        <button class="portBtn">上传头像</button>
    </div>
  </div>
</template>

<script>
export default {
    data(){
        return {
            errInfo:'',
            imgUrl:''
        }
    },
    methods:{
        uploadImg:function(e){
            let $target = e.target || e.srcElement
            let file = $target.files[0]
            //console.log(file);
            if(!/.(jpg|png|jpeg|gif|web|svg)$/.test(file.name)){
                this.errInfo='上传的图片必须是jpg|png|jpeg|gif|web|svg其中的一种'
                this.imgUrl=''
            }
            this.imgUrl=''
            var reader = new FileReader()
            reader.onload = (data) => {
                let res = data.target || data.srcElement
                this.imgUrl = res.result
            }
            reader.readAsDataURL(file)
        }
    }
}
</script>

<style>
.portBox{
    padding: 20px;
}
.portimgBox{
    width: 300px;
    height: 300px;
    background: #fff;
    border: 1px solid #999;
}
.portimg{
    width: 290px;
    height: 290px;
    margin: 5px;
}
.portimg>img{
    width: 100%;
    height: 100%;
}
.errRes{
    height: 20px;
    line-height: 20px;
    text-align: left;
    color: red;
}
.portFun{
    width: 140px;
    height: 40px;
    background: rgb(246, 131, 68);
    margin-top: 50px;
    overflow: hidden;
    border-radius: 5px;
    position: relative;
}
.portFun .portinp{
    opacity: 0;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 999;
    width: 140px;
    height: 40px;
}
.portFun .portBtn{
    width: 140px;
    height: 40px;
    position: absolute;
    top: 0;
    left: 0;
    background: #F68344;
}
</style>